/**
 * Generate code from /{{projectName}}-user/src/main/java/{{packageName}}/core/{{modules}}/service/impl/{{entities@SQL}}ServiceImpl.java.hbs
 */
// @SkipOverwrite
package cn.ibizlab.core.authentication.service.impl;

import cn.ibizlab.core.authentication.config.GlobalLdapConfig;
import cn.ibizlab.core.authentication.constants.AuthenticationConstant;
import cn.ibizlab.core.authentication.domain.AuthProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import static cn.ibizlab.core.authentication.constants.AuthenticationConstant.*;

/**
 * 实体[外部认证] 服务对象接口实现
 *
 * @author generator
 */
@Slf4j
@Service("AuthProviderService")
public class AuthProviderServiceImpl extends AbstractAuthProviderService {

    @Autowired
    GlobalLdapConfig globalLdapConfig;

    @Override
    @Cacheable(value = "authprovider", key = "'dc:'+#p0.dc")
    public List<AuthProvider> all(AuthProvider dto) {
        List<AuthProvider> list = this.list();
        if(globalLdapConfig.enable()) {
            AuthProvider provider = new AuthProvider().setId(PROVIDER_LDAP_DEFAULT_PROVIDER_ID)
                                                      .setName("默认LDAP配置")
                                                      .setAuthenticationType(AuthenticationConstant.PROVIDER_LDAP)
                                                      .setDisabled(0)
                                                      .setAuthenticationUri(globalLdapConfig.getUrls())
                                                      .setSort(999999)
                                                      .setBaseConfig(globalLdapConfig.getDefaultConfigText());
            list.add(provider);
        }
        return list;
    }

    /**
     * 获取优先级最高的认证服务配置
     * @return
     */
    public AuthProvider first(String type) {
        List<AuthProvider> providers = getSelf().all(new AuthProvider());
        if (!ObjectUtils.isEmpty(providers)) {
            Optional<AuthProvider> provider = providers.stream().filter(item -> 0 == item.getDisabled() && type.equals(item.getAuthenticationType())).min(Comparator.comparing(AuthProvider::getSort));
            if (provider.isPresent())
                return provider.get();
        }
        return null;
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = "authprovider", key = "'dc:'+#p0.dc"),
            @CacheEvict(value = "authprovider", key = "'dc:'"),
            @CacheEvict(value = "authprovider", key = "'dc:null'"),
            @CacheEvict(value = "authprovider", key = "'row:'+#p0.id")
    })
    public boolean create(AuthProvider et) {
        return super.create(et);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = "authprovider", key = "'dc:'+#p0.dc"),
            @CacheEvict(value = "authprovider", key = "'dc:'"),
            @CacheEvict(value = "authprovider", key = "'dc:null'"),
            @CacheEvict(value = "authprovider", key = "'row:'+#p0.id")
    })
    public boolean update(AuthProvider et) {
        return super.update(et);
    }

    @Override
    @CacheEvict(value = "authprovider", allEntries = true)
    public boolean remove(AuthProvider et) {
        return super.remove(et);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = "authprovider", key = "'dc:'+#p0.dc"),
            @CacheEvict(value = "authprovider", key = "'dc:'"),
            @CacheEvict(value = "authprovider", key = "'dc:null'"),
            @CacheEvict(value = "authprovider", key = "'row:'+#p0.id")
    })
    public AuthProvider cacheEvict(AuthProvider dto) {
        return super.cacheEvict(dto);
    }
}